package com.gjh.study.mybatis;

import com.gjh.study.mybatis.domain.Person;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author guangjh
 * @version 1.0
 * @date 2020/7/11 9:05
 */
public class Test {


    public static void main(String[] args) throws IOException {
        test3();
    }

    public static void test1() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = sqlSessionFactory.openSession();
        Person p1 = session.selectOne("com.gjh.study.mybatis.mapper.PersonMapper.selectPersonById", 1L);
        Person p2 = session.selectOne("com.gjh.study.mybatis.mapper.PersonMapper.selectPersonById", 1L);
        session.commit();
        session.close();
    }

    public static void test2() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = sqlSessionFactory.openSession();
        Person p1 = session.selectOne("com.gjh.study.mybatis.mapper.PersonMapper.selectPersonById", 1L);
        Person p2 = session.selectOne("com.gjh.study.mybatis.mapper.PersonMapper.selectPersonById", 2L);
        session.commit();
        session.close();
    }

    public static void test3() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        SqlSession session1 = sqlSessionFactory.openSession();
        Person p1 = session1.selectOne("com.gjh.study.mybatis.mapper.PersonMapper.selectPersonById", 1L);

        // 提交后二级缓存才会生效
        session1.commit();
        session1.close();

        SqlSession session2 = sqlSessionFactory.openSession();
        Person p2 = session2.selectOne("com.gjh.study.mybatis.mapper.PersonMapper.selectPersonById", 1L);

        SqlSession session3 = sqlSessionFactory.openSession();
        Person p3 = session2.selectOne("com.gjh.study.mybatis.mapper.PersonMapper.selectPersonById", 1L);


        session2.commit();
        session2.close();
        session3.commit();
        session3.close();
    }

    public static void test4() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = sqlSessionFactory.openSession();

        Person p = null;
        p = session.selectOne("com.gjh.study.mybatis.mapper.PersonMapper.selectPersonById", 1L);
        p.setName("hello");
        session.update("com.gjh.study.mybatis.mapper.PersonMapper.updatePerson", p);

        p = session.selectOne("com.gjh.study.mybatis.mapper.PersonMapper.selectPersonById", 1L);
        session.commit();
        session.close();
    }
}
